package com.cjg.juc;


public class reSyn {
    public static void main(String[] args) {
        final ticket ticket = new ticket();
        new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                ticket.buy();};
            }
            ,"小米").start();
        new Thread(
                ()->{
                    for (int i = 0; i <5 ; i++) {
                        ticket.buy();
                    }},"小米1"
        ).start();  new Thread(
                ()->{
                    for (int i = 0; i <5 ; i++) {
                        ticket.buy();
                    }},"小米2"
        ).start();
    }
}
//使用oop编程不使用 runnable接口从而解耦
class ticket{
    private int tickets=10;
    //核心是 队列加🔒
    public synchronized void buy(){
        if (tickets>0){
            System.out.println(Thread.currentThread().getName()+"卖出了第"+tickets--+"票");
        }

    }
}
